package com.eqs.enterprisequestionnaire.controller;

import com.eqs.enterprisequestionnaire.model.pojo.User;
import com.eqs.enterprisequestionnaire.result.R;
import com.eqs.enterprisequestionnaire.service.EmailService;
import com.eqs.enterprisequestionnaire.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/notify")
public class NotifyController {
    @Autowired
    UserService userService;
    @Autowired
    EmailService emailService;
    //（领导）通知员工
    @PostMapping("/invite")
    public R<String> sendNotification(
            @RequestParam Integer userId,
            @RequestParam String inviteeEmail,
            @RequestParam String questionnaireUrl) {
        User user = userService.getById(userId);
        String subject = "请填写问卷";
        String text = "你好，用户： "+ user.getUserName() +"邀请您填写问卷，\n\n请点击以下链接填写问卷：\n"
                + questionnaireUrl + "\n\n谢谢！";

        String leaderEmail=userService.selectEmailById(userId);
        try {
            emailService.sendSimpleMessage(inviteeEmail, subject, text);
            return R.success("通知已发送至 " + inviteeEmail);
        } catch (Exception e) {
            return R.success("发送通知失败: " + e.getMessage());
        }
    }
}
